package com.army.stock.web;

import com.alibaba.fastjson.JSON;
import com.army.stock.domain.ArmyGood;
import com.army.stock.domain.ArmyMonthplan;
import com.army.stock.domain.ArmyYearplan;
import com.army.stock.service.ArmyMonthplanService;
import com.army.stock.service.ArmyYearplanService;
import com.javalife.common.domain.page.PaginatedArrayList;
import com.javalife.common.web.url.BaseUrl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by zhangyong on 15/2/5.
 * 财务管理相关
 */
@Controller
public class ArmyFinanceController {

    private final static Logger logger = LoggerFactory.getLogger(ArmyFinanceController.class);

    @Autowired
    private ArmyMonthplanService armyMonthplanService;

    private final static String MONTH_PLANS = "finance/monthplans";

    @RequestMapping(value = "/finance/monthplans", method = RequestMethod.GET)
    public String yearplans(HttpServletRequest request, ModelMap modelMap) {
        BaseUrl baseUrl = new BaseUrl(request);
        modelMap.put("baseUrl", baseUrl);
        PaginatedArrayList<ArmyGood> pageInfo = armyMonthplanService.getArmyMonthplanPageInfoForFinance(
                baseUrl.getCurrentPage(),
                20,
                baseUrl.getQueryMap());
        modelMap.put("page", pageInfo);
        modelMap.put("baseUrl", baseUrl);
        return MONTH_PLANS;
    }

    @RequestMapping(value = "/finance/review", method = RequestMethod.POST)
    @ResponseBody
    public String review(HttpServletRequest request, ModelMap modelMap) {
        String yearplanid = request.getParameter("yearplanid");
        String status = request.getParameter("status");
        Map<String, String> returnMap = new HashMap<String, String>();
        try {
            ArmyMonthplan armyMonthplan = new ArmyMonthplan();
            armyMonthplan.setStatus(Integer.valueOf(status));
            armyMonthplan.setId(Long.valueOf(yearplanid));
            armyMonthplanService.reviewArmyMonthplan(armyMonthplan);
            returnMap.put("code", "0");
        } catch (Exception e) {
            returnMap.put("code", "-1");
            logger.error("apply error:", e);
        }
        return JSON.toJSONString(returnMap);
    }

}
